<?php

namespace catchAdmin\registration\controller;

use app\common\utils\Str;
use catchAdmin\registration\service\TrialService;
use catcher\base\CatchController;
use catcher\base\CatchRequest;
use catcher\CatchResponse;
use app\common\model\PatientRecord as PatientRecordModel;
use catcher\exceptions\FailedException;
use catcher\exceptions\ValidateFailedException;

class ClinicalFollowUp extends CatchController
{
    protected $model;

    public function __construct(\app\common\model\ClinicalFollowUp $clinicalFollowUp)
    {
        $this->model = $clinicalFollowUp;
    }

    /**
     * 列表
     *
     * @time 2020年01月09日
     * @param CatchRequest $request
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function index(CatchRequest $request): \think\response\Json
    {
        $type = $request->param("type", 1, "int");
        $patientId = $request->param("patient_id", "", "string");
        if (empty($patientId)) {
            throw new ValidateFailedException("患者ID不能为空");
        }
        return CatchResponse::paginate($this->model->getLists($type, $patientId));
    }

    /**
     * 保存
     *
     * @time 2020年01月09日
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function save(CatchRequest $request): \think\response\Json
    {
        $postData = $request->param();
        $postData['creator_name'] = $request->user()->username;
        return CatchResponse::success($this->model->save($postData));
    }

    /**
     * 更新
     *
     * @time 2020年01月09日
     * @param $id
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function update($id, CatchRequest $request): \think\response\Json
    {
        (new TrialService())->recordOperationLogs(2,1,$id);
        return CatchResponse::success($this->model->updateBy($id, $request->param()));
    }

    /**
     * 删除
     *
     * @time 2020年01月09日
     * @param $id
     * @return \think\response\Json
     */
    public function delete($id): \think\response\Json  //clinicalFollowUp
    {
        (new TrialService())->recordOperationLogs(2,2,$id);
        return CatchResponse::success($this->model->destroy($id));
    }
}